/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.oai.metadataRepository.harvest.tei.svn;

import cz.mzk.oai.metadataRepository.harvest.tei.svnManager.Connector;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.tmatesoft.svn.core.SVNException;

/**
 *
 * @author Martin Řehánek (rehan at mzk.cz)
 */
public class LastRevisionHarvester {

    private static final Logger logger = Logger.getLogger(LastRevisionHarvester.class.getName());
    private final Connector svnConnector;
    private final RecordHarvester recordHarvester;

    LastRevisionHarvester(Connector svnConnector, RecordHarvester recordHarvester) {
        this.svnConnector = svnConnector;
        this.recordHarvester = recordHarvester;
    }

    void harvest(long revision) throws SVNException {
        logger.log(Level.INFO, "harvesting all files from revision {0}", revision);
        List<MetadataDir> dirs = svnConnector.getMetadataDirs(revision);
        System.out.println("dirs: " + dirs.size());
        for (MetadataDir dir : dirs) {
            harvestDir(dir, revision);
        }
    }

    private void harvestDir(MetadataDir dir, long revision) throws SVNException {
        List<String> filesInDir = svnConnector.getAllTeiFilesInDir(dir, revision);
        if (!filesInDir.isEmpty()) {
            logger.log(Level.INFO, "harvesting files in directory {0}", dir.getName());
            //System.out.println("files: " + filesInDir.size());
            for (String filename : filesInDir) {
                recordHarvester.harvestRecord(filename, dir, revision);
            }
        }
    }
}
